package cn.ifanmi.findme.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import cn.ifanmi.findme.bean.User;
import cn.ifanmi.findme.fanconst.NormalString;

public class SPUtil {

	private static final String FIRST_USE = "first_use";
	private static final String HAS_LOGIN = "has_login";
	private static final String DEFAULT_USER = "default_user";
	
	private static final String EMCHATOPTION = "emchatoption";
	public static final String EMCHATOPTION_NOTIFICATION = "notification";
	public static final String EMCHATOPTION_SOUND = "sound";
	public static final String EMCHATOPTION_VIBRATE = "vibrate";
	public static final String EMCHATOPTION_SPEAKER = "speaker";
	
	private static final String PUSH = "push";
	public static final String PUSH_FF = "push_ff";
	public static final String PUSH_SI = "push_si";
	
	/**
	 * 是否第一次使用软件。第一次使用软件要进入引导页面
	 * @param context
	 * @return
	 */
	public static boolean isFirstUse(Context context) {
		SharedPreferences sp = context.getSharedPreferences(FIRST_USE, Context.MODE_PRIVATE);
		String first_use = sp.getString(FIRST_USE, null);
		if (NormalString.Fanmi.VERSION_CODE.equals(first_use)) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 第一次引导页面展示后，第二次开始将不展示。
	 * 软件升级后NormalString.Fanmi.VERSION_CODE要做相应的改变，才能展示新版本引导页面
	 * @param context
	 */
	public static void setFirstUse(Context context) {
		SharedPreferences sp = context.getSharedPreferences(FIRST_USE, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(FIRST_USE, NormalString.Fanmi.VERSION_CODE);
		editor.commit();
	}
	
	public static String getHasLogin(Context context) {
		SharedPreferences sp = context.getSharedPreferences(HAS_LOGIN, Context.MODE_PRIVATE);
		return sp.getString(HAS_LOGIN, null);
	}
	
	public static void setHasLogin(Context context, String value) {
		SharedPreferences sp = context.getSharedPreferences(HAS_LOGIN, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(HAS_LOGIN, value);
		editor.commit();
	}
	
	public static String getUser(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(DEFAULT_USER, Context.MODE_PRIVATE);
		return sp.getString(key, null);
	}
	
	public static void setUser(Context context, String key, String value) {
		SharedPreferences sp = context.getSharedPreferences(DEFAULT_USER, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public static User getDefaultUser(Context context) {
		SharedPreferences sp = context.getSharedPreferences(DEFAULT_USER, Context.MODE_PRIVATE);
		String id = sp.getString(User.ID, null);
		String phoneNumber = sp.getString(User.PHONENUMBER, null);
		String password = sp.getString(User.PASSWORD, null);
		String collegeId = sp.getString(User.COLLEGEID, null);
		String collegeName = sp.getString(User.COLLEGENAME, null);
		String deptId = sp.getString(User.DEPTID, null);
		String deptName = sp.getString(User.DEPTNAME, null);
		String grade = sp.getString(User.GRADE, null);
		String photo = sp.getString(User.PHOTO, null);
		String name = sp.getString(User.NAME, null);
		String sex = sp.getString(User.SEX, null);
		String cl = sp.getString(User.CL, null);
		String nickname = sp.getString(User.NICKNAME, null);
		return new User(id, phoneNumber, password, collegeId, collegeName, 
				deptId, deptName, grade, photo, name, sex, cl, nickname);
	}
	
	public static void emptyDefaultUser(Context context) {
		SharedPreferences sp = context.getSharedPreferences(DEFAULT_USER, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(User.ID, null);
		editor.putString(User.PHONENUMBER, null);
		editor.putString(User.PASSWORD, null);
		editor.putString(User.COLLEGEID, null);
		editor.putString(User.COLLEGENAME, null);
		editor.putString(User.DEPTID, null);
		editor.putString(User.DEPTNAME, null);
		editor.putString(User.GRADE, null);
		editor.putString(User.PHOTO, null);
		editor.putString(User.NAME, null);
		editor.putString(User.SEX, null);
		editor.putString(User.CL, null);
		editor.putString(User.NICKNAME, null);
		editor.commit();
	}
	
	public static void setEMChatOption(Context context, String key, boolean value) {
		SharedPreferences sp = context.getSharedPreferences(EMCHATOPTION, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	public static boolean getEMChatOption(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(EMCHATOPTION, Context.MODE_PRIVATE);
		return sp.getBoolean(key, true);
	}
	
	public static void setPush(Context context, String key, int value) {
		SharedPreferences sp = context.getSharedPreferences(PUSH, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	public static int getPush(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences(PUSH, Context.MODE_PRIVATE);
		return sp.getInt(key, 0);
	}
	
	public static void emptyPush(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PUSH, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putInt(PUSH_FF, 0);
		editor.putInt(PUSH_SI, 0);
		editor.commit();
	}
	
}
